<?php

declare(strict_types=1);

namespace app\middleware;

use app\common\facade\auth\Auth;
use app\common\service\auth\AuthApiService;
use think\facade\Db;

/**
 * 检查是否已经登录.
 */
class ApiUserAuthV2
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     *
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        /**
         * @var AuthApiService
         */

        $user = Auth::attempt(request()->all());

        if (!$user) {
            return fail('还未登录，请先登录', 400);
        }

        if ($user['expired_at'] < time()) {
            return fail('token已过期', 401);
        }


        $request->user = $user;

        return $next($request);
    }
}
